/*
 * @Author: your name
 * @Date: 2020-10-03 09:18:15
 * @LastEditTime: 2020-10-04 12:08:39
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \woniutaofang\lib\pages\home\page\Property\property_page.dart
 */
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:woniutaofang/components/ui/custom_icon.dart';
import 'package:woniutaofang/components/ui/dialog.dart';
import 'package:woniutaofang/pages/home/model/getHouseAlbumByPId.dart';
import 'package:woniutaofang/pages/home/model/getProjectInfoByPId.dart';
import 'package:woniutaofang/pages/home/page/Property/widgets/swiper_widget.dart';
import 'package:woniutaofang/pages/home/widgets/collectionarea_widget.dart';
import 'package:woniutaofang/service/api_service.dart';
import 'package:woniutaofang/utils/toast.dart';
import 'package:woniutaofang/utils/utils.dart';

class PropertyPage extends StatefulWidget {
  final String id;
  PropertyPage(this.id);
  @override
  _PropertyPageState createState() => _PropertyPageState();
}

class _PropertyPageState extends State<PropertyPage>
    with TickerProviderStateMixin {
  List<String> pics = [];
  GetProjectInfoByPIdModel goods;

  double navAlpha = 0;
  double headerHeight;
  ScrollController scrollController = ScrollController();

  TabController controller;
  List<Tab> titleTabs;

  int currentIndex = 0;

  Color c = Colors.grey;

  @override
  void initState() {
    super.initState();

    titleTabs = <Tab>[Tab(text: '详情'), Tab(text: "评论")];

    controller = TabController(length: titleTabs.length, vsync: this)
      ..addListener(() {
        // 监听滑动/点选位置
        if (controller.index.toDouble() == controller.animation.value) {
          setState(() => currentIndex = controller.index);
        }
      });

    headerHeight = Utils.width;

    scrollController.addListener(() {
      var offset = scrollController.offset;
      if (offset <= 0) {
        if (navAlpha != 0) {
          setState(() => navAlpha = 0);
        }
      } else if (offset < headerHeight) {
        if (headerHeight - offset <= Utils.navigationBarHeight) {
          setState(() => c = Colors.white);
        } else {
          c = Colors.grey;
        }
        setState(() => navAlpha = 1 - (headerHeight - offset) / headerHeight);
      } else if (navAlpha != 1) {
        setState(() => navAlpha = 1);
      }
    });

    _getHouseAlbumByPId();
    _getProjectInfoByPId();
  }

  void _getHouseAlbumByPId() async {
    GetHouseAlbumByPIdModel data =
        await ApiService.getHouseAlbumByPId(widget.id);
    if (data != null) {
      setState(() {
        for (var item in data.data.designPicList) {
          pics.add(item.httpPath);
        }
        print(pics);
      });
    } else {}
  }

  void _getProjectInfoByPId() async {
    GetProjectInfoByPIdModel data =
        await ApiService.getProjectInfoByPId(widget.id);
    if (data != null) {
      setState(() {
        goods = data;
      });
    } else {}
  }

  @override
  void dispose() {
    scrollController.dispose();
    // controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    print(widget.id);
    return Scaffold(
      body: _buildBodyView(),
      bottomNavigationBar: _buildBottomView(),
    );
  }

  Widget _buildBodyView() {
    return goods == null
        ? LoadingWidget()
        : CustomScrollView(
            controller: scrollController,
            slivers: <Widget>[
              /// 头部banner
              _buildSliverAppBar(pics),
              // _buildSliverToBoxAdapter(),
              _buildSliverList(context),
            ],
          );
  }

  Widget _buildSliverAppBar(List<String> pics) {
    return SliverAppBar(
      backgroundColor: Colors.red,
      title: Text(
        '楼盘详情',
        style: TextStyle(
          color: Color.fromARGB((navAlpha * 255).toInt(), 255, 255, 255),
        ),
      ),
      pinned: true,
      expandedHeight: 300,
      leading: IconButton(
        icon: Icon(CustomIcon.back, color: c),
        onPressed: () => Navigator.pop(context, '你好'),
      ),
      flexibleSpace:
          FlexibleSpaceBar(background: SwiperHeroWidget(pics, widget.id)),
    );
  }

  Widget _buildSliverToBoxAdapter() {
    /// SliverToBoxAdapter的child属性可以加载普通widget组件
    return SliverToBoxAdapter(
      child: Container(
        alignment: Alignment.center,
        child: Text(
          'SliverToBoxAdapter',
          style: TextStyle(color: Colors.white, fontSize: 24.0),
        ),
        height: 100.0,
        color: Colors.pinkAccent.withOpacity(0.8),
      ),
    );
  }

  Widget _buildSliverList(BuildContext context) {
    /// SliverList类似ListView，他有两种表现形式：SliverChildBuilderDelegate和SliverChildListDelegate，两者区别在于SliverChildBuilderDelegate可以加载不确定数量的列表，而SliverChildListDelegate只能加载固定已知数量。
    return SliverList(
      delegate: SliverChildBuilderDelegate(
        (context, index) {
          return CollectionAreaWidget(
              goods.data.projectName, goods.data.tagList);
        },
        childCount: 10,
      ),
    );
  }

  Widget _buildBottomView() {
    return Container(
      decoration: BoxDecoration(
          border: Border(top: BorderSide(width: 2.0, color: Colors.grey[100]))),
      padding: EdgeInsets.only(
          bottom: Utils.bottomSafeHeight, left: 3.0, right: 3.0),
      child: Row(
        children: <Widget>[
          Container(
            height: 45.0,
            width: ScreenUtil().setWidth(100),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Icon(
                  Icons.share,
                  color: Colors.red,
                  size: 20.0,
                ),
                Text(
                  '分享',
                  style: TextStyle(
                    fontSize: ScreenUtil().setSp(22.0),
                  ),
                ),
              ],
            ),
          ),
          Expanded(
            flex: 2,
            child: Container(
              height: 45.0,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Icon(
                    Icons.image,
                    color: Colors.red,
                    size: 20.0,
                  ),
                  Text(
                    '生产海报',
                    style: TextStyle(
                      fontSize: ScreenUtil().setSp(22.0),
                    ),
                  ),
                ],
              ),
            ),
          ),
          Expanded(
            flex: 2,
            child: Container(
              height: 45.0,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Icon(
                    Icons.home,
                    color: Colors.red,
                    size: 20.0,
                  ),
                  Text(
                    '帮我找房',
                    style: TextStyle(
                      fontSize: ScreenUtil().setSp(22.0),
                    ),
                  ),
                ],
              ),
            ),
          ),
          Expanded(
            flex: 3,
            child: Container(
              height: 36.0,
              child: FlatButton(
                padding: EdgeInsets.all(0.0),
                child: Text(
                  '我要资讯',
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: ScreenUtil().setSp(26.0),
                  ),
                ),
                color: Colors.red,
                onPressed: () {
                  Toast.show(context, '我要资讯');
                },
              ),
            ),
          ),
          SizedBox(
            width: 3.0,
          ),
          Expanded(
            flex: 3,
            child: Container(
              height: 36.0,
              child: FlatButton(
                padding: EdgeInsets.all(0.0),
                child: Text(
                  '我要看房',
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: ScreenUtil().setSp(26.0),
                  ),
                ),
                color: Colors.red,
                onPressed: () {
                  Toast.show(context, '我要看房');
                },
              ),
            ),
          )
        ],
      ),
    );
  }
}
